package cn.com.pansky.util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.sms.model.v20160927.SingleSendSmsRequest;
import com.aliyuncs.sms.model.v20160927.SingleSendSmsResponse;
public class SendMsgUtil {


	/**
	 *校验手机号֤
	 * 
	 * @return
	 */
	public static boolean verifyPhone(String phone) {
		String regExp = "^0?1[3|4|5|7|8]\\d{9}$";
		Pattern p = Pattern.compile(regExp);
		Matcher m = p.matcher(phone);
		return m.find();
	}

	/**
	 * 生成6位随机数
	 * @return
	 */
	public static String generateCode(){
		int random=(int)(Math.random()*1000000);
		String str = ("000000" + random);
		return str.substring(str.length()-6, str.length());
	}
	
	/**
	 * 短信接口
	 * @param destAddr 手机号
	 * @param json 参数
	 * @param templateCode 模板编号
	 * @return
	 * @throws ClientException
	 */
	public Boolean aliSmsComm(String destAddr, JSONObject json, String templateCode) throws ClientException{
		String key = "LTAIbUiebm0JPTk9";
		String sekret = "n70of4INqayQVMBdK5Hmh0QhukXlTR";
		IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou",
				key, sekret);
		DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Sms",
				"sms.aliyuncs.com");
		IAcsClient client = new DefaultAcsClient(profile);
		SingleSendSmsRequest request = new SingleSendSmsRequest();
		try {
			request.setSignName("宝鸡市人社局");
			request.setTemplateCode(templateCode);
			request.setParamString(json.toString());
			request.setRecNum(destAddr);
			SingleSendSmsResponse httpResponse = client.getAcsResponse(request);
			System.out.println(httpResponse);
			System.out.println(httpResponse.getModel());
			return true;
		} catch (ServerException e) {
			e.printStackTrace();
			System.out.println(1);
		} catch (ClientException e) {
			System.out.println(2);
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			SendMsgUtil sendmsg = new SendMsgUtil();
//			Boolean rs=sendmsg.aliSms("17792245955", "9527");
//			String rs=sendmsg.sendMsg("18591950096", "验证码:9527"); 106165590411^1108349005391
//			13892720529 杨迪手机
			JSONObject json = new JSONObject();
			json.put("name", "尉麦彦");
			json.put("idnum", "61**08280783");
			Boolean rs = sendmsg.aliSmsComm("18691958319", json, "SMS_38115048");
			System.out.println(rs);
			
//			 String tel = "612429198908280783";
//			   // 括号表示组，被替换的部分$n表示第n组的内容
//			   tel = tel.replaceAll("(\\d{2})\\d{8}(\\d{8})", "$1**$2");
//			   System.out.print(tel);
		} catch (Exception e) {
			e.printStackTrace();
		}
		}
	}
